#include "useHead.h"
#include "labelTable.h"

int LBLTable::add(const string &name, int tagIndex, int funcIndex)
{
	if (name.empty())
		return -1;

	if (find(name, funcIndex) != -1)
		return -1;

	Label temp;
	temp._name = name;
	temp._index = _vl.size();
	temp._funcIndex = funcIndex;
	temp._tagIndex = tagIndex;
	_vl.push_back(temp);

	return temp._index;
}


int LBLTable::find(const string &name, int funcIndex)
{
	for (int i = 0; i < _vl.size(); ++i)
		if (_vl[i]._funcIndex == funcIndex && _vl[i]._name == name)
			return i;

	return -1;
}	


int LBLTable::get(const string &name, int funcIndex)
{
	int index = find(name, funcIndex);
	if (index == -1)
		return -1;

	return _vl[index]._tagIndex;
}


void LBLTable::dump()
{
    puts("\n+++++++++++++++++<Label Table Dump>++++++++++++++++++++++++\n");
	printf("Label Number :%d\n\n", _vl.size());
	for (int i = 0; i < _vl.size(); ++i) {
		printf("Name:          %s\n", _vl[i]._name.c_str());
		printf("    tagIndex:  %d\n", _vl[i]._tagIndex);
		printf("    funcIndex: %d\n", _vl[i]._funcIndex);
		printf("    index:     %d\n", _vl[i]._index);
	}
    puts("\n+++++++++++++++++<Label Table Dump>++++++++++++++++++++++++\n");
}

